from utils.viewset import CustomModelViewSet
from system.project.models import Channel
from system.project.serializers import ChannelSerializer, ChannelCreateUpdateSerializer
from utils.json_response import SuccessResponse, ErrorResponse


class ChannelViewSet(CustomModelViewSet):
    queryset = Channel.objects.all()
    serializer_class = ChannelSerializer
    create_serializer_class = ChannelCreateUpdateSerializer
    update_serializer_class = ChannelCreateUpdateSerializer

    def partial_update(self, request, *args, **kwargs):
        channel = Channel.objects.filter(id=kwargs.get('pk')).first()
        password = request.data.get('db_password')
        if not channel:
            return ErrorResponse(message='未找到指定记录')
        if password:
            channel.db_password = Channel.make_password(password)
        channel.save()
        return SuccessResponse(data='', message="更新成功")
